package com.css.ids.server.task;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AXIS2Sender<T> {
	private static Log _log = LogFactory.getLog(AXIS2Sender.class);

	private String endpoint;
	private String operationName;
	private Service service;

	public AXIS2Sender() {
		// endpoint = DictMan.getDictType("dataex_d_webservice",
		// "1").getRemark();
		// operationName = DictMan.getDictType("dataex_d_webservice",
		// "2").getRemark();
		service = new Service();
	}

	public AXIS2Sender(String url) {
		service = new Service();
	}

	public AXIS2Sender(String endpoint, String operationName) {
		this.endpoint = endpoint;
		this.operationName = operationName;
		service = new Service();
	}

	@SuppressWarnings("unchecked")
	public T send(Object param, Class<?> clazz, Class<?> returnClazz) throws Exception {
		QName qn = new QName(endpoint, operationName);
		// 创建调用
		Call call = (Call) service.createCall();
		// 注册序列化和反序列化类
		call.registerTypeMapping(clazz, qn, new BeanSerializerFactory(clazz, qn),
				new BeanDeserializerFactory(clazz, qn));
		// 设置返回类型
		call.setReturnType(qn, returnClazz);
		// 设置调用方法名
		call.setOperationName(qn);
		// 设置调用服务来源
		call.setTargetEndpointAddress(endpoint);

		// 调用
		Object ret = call.invoke(new Object[] { param });
		if (_log.isInfoEnabled()) {
			_log.info(ret);
		}
		return (T) ret;
	}

	@SuppressWarnings("unchecked")
	public T send(Object param) throws Exception {
		// 创建调用
		Call call = (Call) service.createCall();
		// 设置调用服务来源
		call.setTargetEndpointAddress(endpoint);
		// 设置调用方法名
		call.setOperationName(new QName(endpoint, operationName));
		// 调用
		Object ret = call.invoke(new Object[] { param });
		if (_log.isDebugEnabled()) {
			_log.debug(ret);
		}
		return (T) ret;
	}

	@SuppressWarnings("unchecked")
	public T send(Class<?> clazz, Class<?> returnClazz) throws Exception {
		QName qn = new QName(endpoint, operationName);
		// 创建调用
		Call call = (Call) service.createCall();
		// 注册序列化和反序列化类
		call.registerTypeMapping(clazz, qn, new BeanSerializerFactory(clazz, qn),
				new BeanDeserializerFactory(clazz, qn));
		// 设置返回类型
		call.setReturnType(qn, returnClazz);
		// 设置调用方法名
		call.setOperationName(qn);
		// 设置调用服务来源
		call.setTargetEndpointAddress(endpoint);

		// 调用
		Object ret = call.invoke(new Object[] {});
		if (_log.isInfoEnabled()) {
			_log.info(ret);
		}
		return (T) ret;
	}

	@SuppressWarnings("unchecked")
	public T send() throws Exception {
		// 创建调用
		Call call = (Call) service.createCall();
		// 设置调用服务来源
		call.setTargetEndpointAddress(endpoint);
		// 设置调用方法名
		call.setOperationName(new QName(endpoint, operationName));
		// 调用
		Object ret = call.invoke(new Object[] {});// new Object[]{ xmlContent }
		if (_log.isDebugEnabled()) {
			_log.debug(ret);
		}
		return (T) ret;
	}

}
